﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using zjx.Wpf.Graffitiboard.Adorners;
using zjx.Wpf.Graffitiboard.Core;

namespace zjx.Wpf.Graffitiboard.Controls
{
    public class GraffitiText : TextBox, ISelectable
    {
        #region 属性
        #region IsSelected
        private bool isSelected = false;

        public bool IsSelected
        {
            get
            {
                return this.isSelected;
            }

            set
            {
                this.isSelected = value;
            }
        }
        #endregion

        #region ID
        private Guid id = Guid.Empty;

        public Guid ID
        {
            get
            {
                return this.id;
            }
        }
        #endregion

        #region SelectionAdorner
        private ResizeRotateAdorner selectionAdorner;

        public ResizeRotateAdorner SelectionAdorner
        {
            get
            {
                return this.selectionAdorner;
            }

            set
            {
                this.selectionAdorner = value;
                //if (this.selectionAdorner != null)
                //{
                //    this.selectionAdorner.PreviewMouseDown += (sender, e) =>
                //    {
                //        if (this.selectionAdorner.IsHitTestVisible)
                //        {
                //            this.selectionAdorner.IsHitTestVisible = false;
                //        }
                //    };
                //}
            }
        }
        #endregion
        #endregion

        #region 构造函数
        public GraffitiText()
        {
            this.AcceptsReturn = true;
            this.Margin = new Thickness(0);
            this.TextWrapping = TextWrapping.Wrap;
            this.HorizontalContentAlignment = HorizontalAlignment.Center;
            this.VerticalContentAlignment = VerticalAlignment.Center;
        }
        #endregion

        #region override
        protected override void OnLostFocus(RoutedEventArgs e)
        {
            base.OnLostFocus(e);
            if (this.SelectionAdorner != null && this.SelectionAdorner.IsHitTestVisible == false)
            {
                this.SelectionAdorner.IsHitTestVisible = true;
            }
        }
        #endregion

        #region 公开方法
        #region GetBounds
        public Rect GetBounds()
        {
            Rect itemRect = VisualTreeHelper.GetDescendantBounds(this);
            Rect itemBounds = this.TransformToAncestor((UIElement)VisualTreeHelper.GetParent(this)).TransformBounds(itemRect);
            return itemBounds;
        }
        #endregion

        #region MoveTo
        /// <summary>
        /// 移动到指定位置
        /// </summary>
        /// <param name="offsetX">水平偏移量</param>
        /// <param name="offsetY">垂直偏移量</param>
        public void MoveTo(double offsetX, double offsetY)
        {
            Point ptChange = this.RenderTransform.Transform(new Point(offsetX, offsetY));
            double left = InkCanvas.GetLeft(this);
            double top = InkCanvas.GetTop(this);

            if (double.IsNaN(left)) left = 0;
            if (double.IsNaN(top)) top = 0;

            InkCanvas.SetLeft(this, left + ptChange.X);
            InkCanvas.SetTop(this, top + ptChange.Y);
        }
        #endregion
        #endregion
    }
}
